package com.symbio.ishenapi.background.controller;

import com.jfinal.plugin.activerecord.Db;
import com.symbio.ishenapi.background.model.TmDoctorInsuranceCorps;
import com.symbio.ishenapi.background.model.TmInsuranceCorps;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;


import java.math.BigInteger;
import java.util.List;

/**
 * 保险团
 */
@RequestMapping("/tmDoctorInsuranceCorpsController")
public class TmDoctorInsuranceCorpsController extends BaseController {

    TmInsuranceCorps tmInsuranceCorps = new TmInsuranceCorps();

    /**
     * 保存保险团和医生关系表
     */
    public  void save(){
        TmDoctorInsuranceCorps tmDoctorInsuranceCorps = (TmDoctorInsuranceCorps)getReqBodyObject(TmDoctorInsuranceCorps.class);
        if(tmDoctorInsuranceCorps.getDoctorId() == null || tmDoctorInsuranceCorps.equals("")){
            renderJsonError(40003, "医生编号");
        }else if(tmDoctorInsuranceCorps.getInsuranceCorpsId() == null || tmDoctorInsuranceCorps.equals("")){
            renderJsonError(40003, "保险团编号");
        }
        tmDoctorInsuranceCorps.setStatus(1L);
        if(tmDoctorInsuranceCorps.save()){
          TmInsuranceCorps tic = tmInsuranceCorps.getByid(tmDoctorInsuranceCorps.getInsuranceCorpsId());
            Long num = tic.getInsuranceCorpsNumber()+1;
            tic.setInsuranceCorpsNumber(num);
            tic.update();
            renderJsonRet(40001);
        }else{
            renderJsonError(40003);
        }
    }

    /**
     * 修改已读状态,单条
     */
    public void updateStatus() {
        TmDoctorInsuranceCorps user = (TmDoctorInsuranceCorps) getReqBodyObject(TmDoctorInsuranceCorps.class);
        TmDoctorInsuranceCorps u = TmDoctorInsuranceCorps.dao.findById(user.getId());
        if (u != null) {
            u.setStatus(0L);
            if (u.update()) {
                TmInsuranceCorps tic = tmInsuranceCorps.getByid(u.getInsuranceCorpsId());
                Long num = tic.getInsuranceCorpsNumber()-1;
                tic.setInsuranceCorpsNumber(num);
                tic.update();
                renderJsonRet(user.getId());
            } else {
                renderJsonError(40005);
            }
        } else {
            renderJsonError(40003);
            return;
        }
    }



    /**
     * 修改删除字段，批量
     */
    public void updateDoctorInsuranceCorpsList() {
        //TmDoctorInsuranceCorps user = (TmDoctorInsuranceCorps) getReqBodyObject(TmDoctorInsuranceCorps.class);
        String id = getPara("id");
        List<TmDoctorInsuranceCorps> tmDoctorInsuranceCorpsoList = TmDoctorInsuranceCorps.dao.findByIdList(id);
      for (TmDoctorInsuranceCorps tmDoctorInsuranceCorps : tmDoctorInsuranceCorpsoList) {
            tmDoctorInsuranceCorps.setStatus(0L);
            if (tmDoctorInsuranceCorps.update()) {
                renderJsonRet(40001);
            } else {
                renderError(40002);
            }
        }

    }
}
